package flc.ast.fragment;

import android.annotation.SuppressLint;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import flc.ast.R;
import flc.ast.activity.FileTransferActivity;
import flc.ast.adapter.ContactsAdapter;
import flc.ast.bean.SelectFileBean;
import flc.ast.databinding.FragmentContactsBinding;
import flc.ast.utils.SendFileManager;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.bean.ContactInfo;
import stark.common.basic.utils.ContactUtil;
import stark.common.basic.utils.RxUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link ContactsFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ContactsFragment extends BaseNoModelFragment<FragmentContactsBinding> {

    private SendFileManager mInstance;
    private ContactsAdapter contactsAdapter;


    @Override
    protected int onCreate() {
        return R.layout.fragment_contacts;
    }

    @Override
    protected void initView() {
        mInstance = SendFileManager.getInstance();
        //联系人
        mDataBinding.rvContactsList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        contactsAdapter = new ContactsAdapter();
        mDataBinding.rvContactsList.setAdapter(contactsAdapter);
        contactsAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        boolean hasExist = false;
        int tmpPosition = -1;
        for (SelectFileBean s : mInstance.getSelectFileBeans()) {
            tmpPosition++;
            if (contactsAdapter.getItem(position).getPhone().equals(s.getPath())) {
                hasExist = true;
                break;
            }
        }
        if (hasExist) {
            contactsAdapter.getItem(position).setSelected(false);
            mInstance.deleteSelectFile(tmpPosition);
        } else {
            contactsAdapter.getItem(position).setSelected(true);
            SelectFileBean bean = new SelectFileBean();
            bean.setType(5);
            bean.setName(contactsAdapter.getItem(position).getName());
            bean.setPath(contactsAdapter.getItem(position).getPhone());
            bean.setSize(contactsAdapter.getItem(position).getPhone().length());
            mInstance.addSelectFile(bean);
        }
        ((FileTransferActivity) mContext).refreshDataCount(mInstance);
        contactsAdapter.notifyDataSetChanged();
    }

    @Override
    protected void initData() {
        getContactsData();
    }

    private void getContactsData() {
        RxUtil.create(new RxUtil.Callback<List<ContactInfo>>() {
            @SuppressLint("MissingPermission")
            @Override
            public void doBackground(ObservableEmitter<List<ContactInfo>> emitter) {
                List<ContactInfo> contactInfoList = ContactUtil.getAllContact();
                emitter.onNext(contactInfoList);
            }

            @Override
            public void accept(List<ContactInfo> contactInfoList) {
                if (contactInfoList != null && contactInfoList.size() != 0) {
                    mDataBinding.rvContactsList.setVisibility(View.VISIBLE);
                    mDataBinding.tvContactsNoData.setVisibility(View.GONE);
                    contactsAdapter.setList(contactInfoList);
                } else {
                    mDataBinding.rvContactsList.setVisibility(View.GONE);
                    mDataBinding.tvContactsNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

}